package com.cn.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.function.Consumer;

/**
 * @author: chenyuncong
 * @Date: 2020/3/9 17:19
 */

@Component
@Slf4j
public class AuthSignatureFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String token = exchange.getRequest().getQueryParams().getFirst("authToken");
        String version = exchange.getRequest().getQueryParams().getFirst("version");
        log.info("token:[{}]",token);
        //向headers中放文件，记得build
//        ServerHttpRequest host = exchange.getRequest().mutate().header("a", "888").build();
        Consumer<HttpHeaders> httpHeaders = httpHeader -> {
            httpHeader.set("aaaa", "bbb");
            httpHeader.set("xxx", "cc");
            httpHeader.set("bbxx", "bbbx");
            httpHeader.set("aaaa", "bbb");
        };
        ServerHttpRequest serverHttpRequest = exchange.getRequest().mutate().headers(httpHeaders).build();

        //将现在的request 变成 change对象
        ServerWebExchange build = exchange.mutate().request(serverHttpRequest).build();

        return chain.filter(build);
    }

    @Override
    public int getOrder() {
        return 10000;
    }
}
